package xiaye.crawler.domain;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.LinkedList;
import java.util.List;

import javax.persistence.CollectionTable;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.OrderColumn;
import javax.persistence.Transient;

import org.springframework.transaction.annotation.Transactional;

import com.google.inject.Inject;

@Entity
public class Page {
	@Transient
	private EntityManager entityManager;
	private int id;
	private URI startUri;
	private URI finalUri;
	private String encoding;
	private String content;
	private List<URI> outlinks;
	
	@Inject
	public Page(EntityManager entityManager){
		this.entityManager = entityManager;
	}
	
	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	
	@Column(length = 512)
	public String getStartUriString() {
		return startUri.toString();
	}
	public void setStartUriString(String startUri) {
		try {
			this.startUri = new URI(startUri);
		} catch (URISyntaxException e) {
			e.printStackTrace();
		}
	}
	
	@Column(length = 512)
	public String getFinalUriString() {
		return finalUri.toString();
	}
	public void setFinalUriString(String finalUri) {
		try {
			this.finalUri = new URI(finalUri);
		} catch (URISyntaxException e) {
			e.printStackTrace();
		}
	}
	
	@Column(length = 32)
	public String getEncoding() {
		return encoding;
	}
	public void setEncoding(String encoding) {
		this.encoding = encoding;
	}
	
	@Lob
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
	
	@ElementCollection
	@OrderColumn
	@Column(length = 512)
	public List<String> getOutlinksString() {
		if (outlinks == null){
			return null;
		}
		List<String> results = new LinkedList<String>();
		for (URI item : outlinks){
			results.add(item.toString());
		}
		return results;
	}
	public void setOutlinksString(List<String> outlinks) {
		if (outlinks == null){
			this.outlinks = null;
			return;
		}
		List<URI> results = new LinkedList<URI>();
		try {
			for (String item : outlinks){
				results.add(new URI(item));
			}
		} catch (URISyntaxException e) {
			e.printStackTrace();
		}
	}
	
	@Transient
	public URI getStartUri() {
		return startUri;
	}
	public void setStartUri(URI startUri) {
		this.startUri = startUri;
	}

	@Transient
	public URI getFinalUri() {
		return finalUri;
	}
	public void setFinalUri(URI finalUri) {
		this.finalUri = finalUri;
	}

	@Transient
	public List<URI> getOutlinks() {
		return outlinks;
	}
	public void setOutlinks(List<URI> outlinks) {
		this.outlinks = outlinks;
	}

	synchronized public void persist(){
		entityManager.getTransaction().begin();
		entityManager.persist(this);
		entityManager.getTransaction().commit();
	}
}
